package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.BabyInfo;
import cn.b.training.baby_education_training.entity.BabyNote;
import cn.b.training.baby_education_training.repository.BabyInfoRepository;
import cn.b.training.baby_education_training.repository.BabyNoteRepository;
import cn.b.training.baby_education_training.service.BabyNoteService;
import cn.b.training.baby_education_training.service.dto.BabyNoteDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-04 13:10
 **/
@Service
public class BabyNoteServiceImpl implements BabyNoteService {
    @Autowired
    private BaiduAiService baiduAiService;
    @Autowired
    private BabyNoteRepository babyNoteRepository;
    @Autowired
    private BabyInfoRepository babyInfoRepository;

    @Override
    public boolean addNote(BabyNoteDto babyNoteDto) {
        BabyInfo babyInfo = babyInfoRepository.getByBabyId(babyNoteDto.getBabyId());
        BabyNote babyNote = new BabyNote();
        //if (babyNoteDto.getImage() != null) {
        //    String fileKey = UUID.randomUUID().toString();
        //    String fileName = babyNoteDto.getImage().getOriginalFilename();
        //    if (fileName.indexOf(".") > 0) {
        //        String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
        //        fileKey += extName;
        //    }
        //    try {
        //        baiduAiService.putFile3(fileKey, babyNoteDto.getImage().getInputStream());
        //    } catch (Exception e) {
        //        e.printStackTrace();
        //    }
        //    babyNote.setImage("http://hzw-bos.bj.bcebos.com/note/" + fileKey);
        //}
        babyNote.setImage(babyNoteDto.getImage());
        babyNote.setBabyId(babyInfo.getId());
        babyNote.setTitle(babyNoteDto.getTitle());
        babyNote.setContent(babyNoteDto.getContent());
        babyNote.setPublishTime(babyNoteDto.getPublishTime());
        babyNote = babyNoteRepository.save(babyNote);
        if (babyNote!=null){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateNoteInfo(BabyNoteDto babyNoteDto) {
        BabyNote babyNote = babyNoteRepository.getNoteById(babyNoteDto.getId());
        //if (babyNoteDto.getImage() != null) {
        //    String fileKey = UUID.randomUUID().toString();
        //    String fileName = babyNoteDto.getImage().getOriginalFilename();
        //    if (fileName.indexOf(".") > 0) {
        //        String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
        //        fileKey += extName;
        //    }
        //    try {
        //        baiduAiService.putFile3(fileKey, babyNoteDto.getImage().getInputStream());
        //    } catch (Exception e) {
        //        e.printStackTrace();
        //    }
        //    babyNote.setImage("http://hzw-bos.bj.bcebos.com/note/" + fileKey);
        //}
        babyNote.setImage(babyNoteDto.getImage());
        babyNote.setPublishTime(babyNoteDto.getPublishTime());
        babyNote.setTitle(babyNoteDto.getTitle());
        babyNote.setContent(babyNoteDto.getContent());
        babyNoteRepository.save(babyNote);
        if (babyNote!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<BabyNote> getNoteByBabyId(Long babyId) {
        List<BabyNote> babyNote = babyNoteRepository.getNoteByBabyId(babyId);
        return babyNote;
    }

    @Override
    public BabyNote getNoteById(Long id) {
        BabyNote babyNote = babyNoteRepository.getNoteById(id);
        return babyNote;
    }

    @Override
    public Boolean deleteNoteById(Long id) {
        try{
            this.babyNoteRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<BabyNote> getNoteByTitle(String title) {
        return babyNoteRepository.getNoteByTitle(title);
    }
}
